package com.zhajh.utils;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.Rect;
import android.text.TextUtils;

public class Player
{
    public interface Status
    {
        public static final int UNRIPE = 0;     // 未准备
        public static final int IDLE = 1;       // 在观看(已就座)
        public static final int READY = 2;      // 已准备
        public static final int PLAYING = 3;    // 正在玩中
    }

    public interface Direction
    {
        // 状态提示区在基本信息区的x边
        public static final int LEFT = 1;    // 左
        public static final int TOP = 2;     // 上
        public static final int RIGHT = 3;    // 右
        public static final int BOTTOM = 4;    // 下
    }

	private static final int NAME_VIEW_H = 26;
	private static final int MONEY_VIEW_H = 26;
    private static final int STATUS_BG_W = 93;	// 状态提示区宽度
    private static final int STATUS_BG_H = 66;	// 状态提示区高度
    // 状态提示区: 内容区边距
    private static final int STATUS_CONTENT_MARGIN_LEFT = 1;	// 左边距
    private static final int STATUS_CONTENT_MARGIN_TOP = 1;		// 上边距
    private static final int STATUS_CONTENT_MARGIN_RIGHT = 3;	// 右边距
    private static final int STATUS_CONTENT_MARGIN_BOTTOM = 18;	// 下边距

	private Context mContext;
	private Rect mBaseRect = new Rect();	// 基本信息区: 名称 + 头像 + 金币
	private Rect mStatusRect = new Rect();	// 状态信息区
	private Rect mStatusContentRect = new Rect();	// 状态信息内容区
	private Rect mAllRect = new Rect();
	private Bitmap mBmpHead = null;
	private Paint mInfoPaint = null;
	private FontMetrics mInfoFontMetrics;
	private int mStatusDirection = 0;   // 状态区的方向

    private String mUsername = null;
    private String mNickname = null;
    private int mStatus = Status.UNRIPE;
	private int mMoney = 0;     // 金币数量
    private int mLevel = 0;     // 等级, grade
    private int mHeadId = 0;
	private int mCard1 = 0;     // 牌1
    private int mCard2 = 0;     // 牌2
    private int mCard3 = 0;     // 牌3

    private boolean mIsRobot = false;   // 是否机器人
    private boolean mIsView = false;    // 是否看牌
    private int mServerSeatId = 0;		// 在服务端的座位序号

	public Player(Context context) {
		mContext = context;

		mInfoPaint = new Paint();
        mInfoPaint.setAntiAlias(true);
        mInfoPaint.setColor(Color.WHITE);
        mInfoPaint.setTextSize(16);
        // 居中对齐
        mInfoPaint.setTextAlign(Align.CENTER);

        mInfoFontMetrics = mInfoPaint.getFontMetrics();
	}

	public Rect rect()
	{
		return mAllRect;
	}

	public Rect baseRect()
	{
		return mBaseRect;
	}

	public Rect statusRect()
	{
		return mStatusRect;
	}

	// 基本信息区: 名称 + 头像 + 金币
	public void setBaseRect(int x, int y, int w, int h, int direction)
	{
	    mStatusDirection = direction;

		mBaseRect.left = x;
		mBaseRect.top = y;
		mBaseRect.right = x + w;
		mBaseRect.bottom = y + h;

		if (direction == Direction.LEFT)
		{
			mStatusRect.left = mBaseRect.left - STATUS_BG_W;
		}
		else
		{
			mStatusRect.left = mBaseRect.right;
		}

		mStatusRect.top = mBaseRect.centerY()-STATUS_BG_H;
		mStatusRect.right = mStatusRect.left + STATUS_BG_W;
		mStatusRect.bottom = mStatusRect.top + STATUS_BG_H;

		mAllRect.set(mBaseRect);
		mAllRect.union(mStatusRect);

		mStatusContentRect.left = mStatusRect.left + STATUS_CONTENT_MARGIN_LEFT;
		mStatusContentRect.top = mStatusRect.top + STATUS_CONTENT_MARGIN_TOP;
		mStatusContentRect.right = mStatusRect.right - STATUS_CONTENT_MARGIN_RIGHT;
		mStatusContentRect.bottom = mStatusRect.bottom - STATUS_CONTENT_MARGIN_BOTTOM;

		PlayerResFactory.init(w, h, NAME_VIEW_H, MONEY_VIEW_H);
		PlayerResFactory.ensureInitAllRes(mContext);

		if (isEmpty() || mHeadId==0)
		{
			mBmpHead = PlayerResFactory.bmpDefaultHead;
		}
		else
		{
			int hh = h - NAME_VIEW_H - MONEY_VIEW_H;
			mBmpHead = PlayerResFactory.ensureInitBitmap(mBmpHead, mContext, w, hh, mHeadId);
		}
	}

	public int getStatus()
	{
		return mStatus;
	}

	// 是否空座位
	public boolean isEmpty()
	{
		return TextUtils.isEmpty(mUsername);
	}

	public int getServerSeatId()
	{
	    return mServerSeatId;
	}

	public void setServerSeatId(int id)
	{
	    mServerSeatId = id;
	}

	public void clear()
	{
	    mUsername = null;
	    mStatus = Status.UNRIPE;
	    mMoney = 0;
	    mHeadId = 0;
	    mBmpHead = PlayerResFactory.bmpDefaultHead;
	}

	public void draw(Canvas canvas)
	{
		int y = mBaseRect.top;
		canvas.drawBitmap(PlayerResFactory.bmpNameBg, mBaseRect.left, y, null);
		y += PlayerResFactory.bmpNameBg.getHeight();
		canvas.drawBitmap(mBmpHead, mBaseRect.left, y, null);
		y += mBmpHead.getHeight();
		canvas.drawBitmap(PlayerResFactory.bmpMoneyBg, mBaseRect.left, y, null);

        if (!isEmpty())
        {
    		// 打印名字
            //float fontHeight = mInfoFontMetrics.bottom - mInfoFontMetrics.top;    // 文字高度
            float tx = mBaseRect.centerX();
            float ty = mBaseRect.top + NAME_VIEW_H/2 + mInfoFontMetrics.bottom;
            canvas.drawText(mNickname, tx, ty, mInfoPaint);

            // 金币
            ty = mBaseRect.bottom - MONEY_VIEW_H/2 + mInfoFontMetrics.bottom;
            canvas.drawText(""+mMoney, tx, ty, mInfoPaint);
        }

        // status info
        if (mStatus == Status.READY)
        {
            drawStatus(canvas, PlayerResFactory.bmpOK);
        }
	}

	private void drawStatus(Canvas canvas, Bitmap bitmap)
	{
		Bitmap bg = null;
		
	    if (mStatusDirection == Direction.LEFT)
	    	bg = PlayerResFactory.bmpStatusBgLeft;
	    else
	    	bg = PlayerResFactory.bmpStatusBgRight;

	    canvas.drawBitmap(bg, mStatusRect.left, mStatusRect.top, null);
        drawStatusContent(canvas, bitmap);
	}

	private void drawStatusContent(Canvas canvas, Bitmap bitmap)
	{
		int w = bitmap.getWidth();
		int h = bitmap.getHeight();
		int x = mStatusContentRect.centerX() - w/2;
		int y = mStatusContentRect.centerY() - h/2;
		canvas.drawBitmap(bitmap, x, y, null);
	}

	public void setInfo(JSONObject obj) throws JSONException
	{
		mUsername = obj.getString("username");
		mNickname = obj.getString("nickname");
		mStatus = obj.getInt("status");
		mMoney = obj.getInt("money");
		mLevel = obj.getInt("level");
		mIsView = obj.getBoolean("is_view");
		mIsRobot = obj.getBoolean("is_robot");
		mServerSeatId = obj.getInt("seat_id");

		if (TextUtils.isEmpty(mNickname))
		{
		    mNickname = mUsername;
		}

		int headId = 0;

		if (mBaseRect.height() > 0 && mHeadId!=headId && headId>0)
		{
			if (mBmpHead != null)
			{
				mBmpHead.recycle();
				mBmpHead = null;
			}

			int hh = mBaseRect.height() - NAME_VIEW_H - MONEY_VIEW_H;
			mBmpHead = PlayerResFactory.ensureInitBitmap(mBmpHead, mContext, mBaseRect.width(), hh, headId);
		}

		mHeadId = headId;
	}

	public void idle()
	{
		mStatus = Status.IDLE;
	}

	public void ready()
	{
		mStatus = Status.READY;
	}

	public void play()
	{
		mStatus = Status.PLAYING;
	}
}
